package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpExpr;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 员工管理
 */
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Resource
    private EmpExprMapper empExprMapper;

    @Override
    public PageBean<Emp> pageQuery(String name, Integer gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {
        //开启分页
        PageHelper.startPage(page,pageSize);
        //查询列表：不需要传参起始索引与查询数量
        Page<Emp> empPage = empMapper.page(name,gender,begin,end);
        //封装结果
//        Page<Emp> p = (Page<Emp>) empPage;

        return new PageBean<>((int) empPage.getTotal(),empPage);
    }

    //添加员工
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Emp emp) throws Exception {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        emp.setPassword("123456");

        empMapper.insert(emp);

//        if (true){
//            throw new Exception("人造编译时异常");
//        }

        //插入员工经历
        List<EmpExpr> exprLists = emp.getExprList();
        if (exprLists != null && !exprLists.isEmpty()){
            for (EmpExpr empList : exprLists) {
                empList.setEmpId(emp.getId());
            }
            empExprMapper.batchInsert(exprLists);
        }

    }

    //根据id删除员工
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(List<Integer> ids) {
        if (ids == null || ids.isEmpty()){
            return;
        }
        //删除员工信息
        empMapper.deleteByIds(ids);
        //删除员工的工作经历
        empExprMapper.deleteByEmpIds(ids);

    }

    //修改员工-查询回显
    @Override
    public Emp getById(Integer id) {
        Emp emp = empMapper.selectById(id);
        return emp;
    }

    //修改员工
    @Override
    public void putEmp(Emp emp) {
        //更新员工信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmp(emp);

        //更新员工的工作经历
        //先删除该员工所有经历
        Integer id = emp.getId();
        List<Integer> ids = new ArrayList<>();
        ids.add(id);
        empExprMapper.deleteByEmpIds(ids);
        //  重新添加工作经历
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList == null || exprList.isEmpty()){
            return;
        }
            //添加员工id
            for (EmpExpr empExpr : exprList) {
                empExpr.setEmpId(emp.getId());
            }
        empExprMapper.batchInsert(exprList);
    }

    /**
     * 查询全部员工
     */
    @Override
    public List<Emp> getAllEmp() {
        List<Emp> empLists = empMapper.selectAllEmp();
        return empLists;
    }
}